<?php
/**
 * Description of CommonLoginForm
 *
 * @author Animus Inc.
 */

// Constants

define('DOMROOT', dirname(dirname(__FILE__)));

// Imports
require_once DOMROOT.'/classes/FormsAPI/Form.php';
require_once DOMROOT.'/classes/FormsAPI/TextField.php';
require_once DOMROOT.'/classes/FormsAPI/PasswordField.php';

class AdminLoginForm extends Form{
    
    public function __construct() {
        
        parent::__construct("AdminLoginForm", "AdminLoginForm", "AdminLoginForm", "");
     
        // User Name Field
        $_username = new TextField("_username");
        $_username->isRequired();
        $_username_handler = $_username->getHTMLHandler();
        $attrs_username = array("name" => $_username->getName(), "class" => "_username", "id" => "_username");
        $_username_handler->setAttrs($attrs_username);
        $_username_handler->setLabel("Username : ");
        
        // Password Field
        $_password = new PasswordField("_password");
        $_password->isRequired();
        $_password_handler = $_password->getHTMLHandler();
        $attrs_password = array("name" => $_password->getName(), "class" => "_password", "id" => "_password");
        $_password_handler->setAttrs($attrs_password);
        $_password_handler->setLabel("Password : ");
        
        parent::setMethod("POST");
        
        parent::addField($_username);
        parent::addField($_password);
        parent::setTarget('/stockworldmarket/admin/login.php');
        
        return parent;
    }
    
    public function setFieldValue($key, $value) {
      
        parent::setFieldValue($key, $value);
    }
    
    public function setCommonError($_error){
        
        parent::setCommonError($_error);
    }
    
    public function validate() { 
       $fields = parent::getFields();
       foreach($fields as $field){
           $name = $field -> getName();
           switch($name){
               case ("_username") : {
                                    $val_error_flag = true;
                                    $value = $field->getValue();
                                    if(strlen($value) == 0){
                                        $val_error_flag = $field -> validate();
                                        if(!$val_error_flag){
                                            return $val_error_flag;
                                        }
                                    }
                                    else{
                                             if($val_error_flag){
                                                $error_flag = $this ->cleanup($field -> getValue());
                                                if($error_flag){  
                                                   $field -> setDefaultError("* No Special Characters");
                                                    $val_error_flag = false;
                                                }
                                             }
                                           return $val_error_flag;  
                                    }
                }
               case ("_password") : {
                                    $val_error_flag = true;
                                    $value = $field->getValue();
                                    if(strlen($value) == 0){
                                        $val_error_flag = $field -> validate();
                                        if(!$val_error_flag){
                                            return $val_error_flag;
                                        }
                                  }    
                }
        }
       } 
            return $val_error_flag;
    }

    
    public function getMethod() {
        
        return parent::getMethod();
    }
    
    public function handleSubmit($args){
        
	 return parent::handleSubmit($args);
 
    }
    
    public function cleanup($args){ // Cleanup all special characters except underscore "_"
        
        $_spec_chars = array("`","!","@","#","$","%","^","&","*","(",")","-","+","=","{","}","[","]","|","\\","/",",","<",">",".",":",";","\'","\""," ");
        foreach($_spec_chars as	$_spec_char){			
            $pos = strpos( $args, $_spec_char);	
            if($pos !== false){
             $_error_flag = true;
             break;
             }
            else
             $_error_flag = false;
        }
        return $_error_flag;
    }
}

?>
